package com.faiz.picturebed.controller;


import com.faiz.picturebed.common.R;
import com.faiz.picturebed.service.PictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 图床 前端控制器
 * </p>
 *
 * @author faiz
 * @since 2021-07-14
 */
@Api(value = "图床工具")
@RestController
@RequestMapping("/picture")
public class PictureController {

    private final PictureService pictureService;

    @Autowired
    public PictureController(PictureService pictureService) {
        this.pictureService = pictureService;
    }

    @ApiOperation(value = "上传图片文件")
    @PostMapping(value = "/uploadPicture", headers = "content-type=multipart/form-data")
    public R uploadPicture(@ApiParam(value = "file", required = true) @RequestPart("file") MultipartFile file) {
        String url = null;
        try {
//            PictureService pictureService = SpringUtils.getBean(PictureService.class);
            url = pictureService.uploadPicture(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().data("url", url);
    }
}

